package com.jule.teenpatti.dispacher.service;


import com.jule.teenpatti.dispacher.dao.DBUtil;
import com.jule.teenpatti.dispacher.dao.bean.VersionConfigModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class VersionService {
    VersionConfigModel versionConfigModel;

    public VersionService() {
        init();
    }

    private static class SingletonHolder {
        protected static final VersionService instance = new VersionService();
    }

    public static final VersionService getInstance() {
        return VersionService.SingletonHolder.instance;
    }

    /**
     * 初始化加载内存
     */
    public boolean init() {
        versionConfigModel = DBUtil.loadVersionConfig();
        if (versionConfigModel == null) {
            throw new Error("VersionConfigModel is null");
        }
        return true;
    }

    /**
     * 判断是否可强更
     *
     * @param downPlatform
     * @return true可强更false不可强更
     */
    public String getDownPlatform(String downPlatform) {
        if (!StringUtils.isNotEmpty(downPlatform)) {
            log.error("judageVersion()，downPlatform:{}", downPlatform);
            return "";
        }
        versionConfigModel = DBUtil.loadVersionConfigByDownPlatform(downPlatform);

        if (versionConfigModel == null) {
            return "";
        }
        return versionConfigModel.getDownloadLink();
    }

    /**
     * 判断是否可强更
     *
     * @param version
     * @return true可强更false不可强更
     */
    public boolean judageVersion(String version, String downPlatform) {
        try {
            if (!StringUtils.isNotEmpty(version) || !StringUtils.isNotEmpty(downPlatform)) {
                log.info("judageVersion()，version:{},downPlatform:{}", version, downPlatform);
                return false;
            }
            versionConfigModel = DBUtil.loadVersionConfigByDownPlatform(downPlatform);

            if (versionConfigModel == null) {
                return false;
            }

            String[] v_str = version.split("\\.");// 1.2.0.0
            if (v_str.length < 3) {
                log.error("judageVersion()，version args is error. version:{}", version);
                return true;
            }

            String[] v_str_now = versionConfigModel.getVersion().split("\\.");
            int a = 0;
            if (v_str_now.length >= 4) {
                a = Integer.parseInt(v_str_now[0].trim()) * 1000000 + Integer.parseInt(v_str_now[1].trim()) * 10000
                        + Integer.parseInt(v_str_now[2].trim()) * 100 + Integer.parseInt(v_str_now[3].trim());
            }

            int b = 0;
            if (v_str.length >= 4) {
                b = Integer.parseInt(v_str[0].trim()) * 1000000 + Integer.parseInt(v_str[1].trim()) * 10000
                        + Integer.parseInt(v_str[2].trim()) * 100 + Integer.parseInt(v_str[3].trim());
            }

            if (a > b) {
                return true;
            }
        } catch (Exception ex) {
            log.error("judageVersion(),downPlatform:{},version:{}", downPlatform, version);
            log.error(ex.getMessage(), ex);
        }
        return false;
    }
}
